﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Xml;

namespace Thurn_und_Taxis
{
    /// <summary>
    /// Interaction logic for HelpForm.xaml
    /// </summary>
    public partial class HelpForm: Window
    {
        private String helpFileName = "resources/help/help.xml";
        private String TAG_MAIN = "main";
        private String TAG_SETTINGS = "settings";
        private String TAG_MAP = "map";
        private String TAG_STARTGAME = "startgame";
        private String TAG_USERTURN = "userturn";
        private String TAG_PICKCARD = "pickcard";
        private String TAG_PLACECARDONROUTE = "placecardonroute";
        private String TAG_SCORE = "score";
        private String TAG_DISCARDCARDS = "discardcards";
        private String TAG_ENDTURN = "endturn";
        private String TAG_TOWNSANDROUTES = "townsandroutes";
        private String TAG_BONUSESANDSTAGECOACHES = "bonusesandstagecoaches";
        private String TAG_GETHELP = "gethelp";
        private String TAG_ENDGAME = "endgame";
        private String settings_image = "resources/help/settings.png";
        private String game_img = "resources/help/gameTTX.png";
        private String gameboard_img = "resources/help/gameboard.png";
        private String route_img = "resources/help/route.png";
        private String sixcards_img = "resources/help/sixcards.png";
        private String cards_img = "resources/help/cards.png";
        private String buttons_img = "resources/help/buttons.png";
        private String helpCh_img = "resources/help/helpCh.png";
        private String routes_img = "resources/help/routes.png";
        private String bonuses_img = "resources/help/bonuses.png";
        private String startgame_img = "resources/help/startgame.png";
        private String help_title = "Thurn und Taxis - Help Page";
        private String settings_title = "Thurn und Taxis - Settings";
        private String gameMap_title = "Thurn und Taxis - Game Map";
        private String startGame_title = "Thurn und Taxis - Start Game";
        private String userThurn_title = "Thurn und Taxis - User Thurn";
        private String getHelp_title = "Thurn und Taxis - Get Help";
        private String towns_title = "Thurn und Taxis - Towns and Routes";
        private String bonuses_title = "Thurn und Taxis - Bonuses and Coaches";
        private String endGame_title = "Thurn und Taxis - End Game";

        public HelpForm()
        {
            InitializeComponent();
            LoadHelpContent(TAG_MAIN);
            loadHelpImage(HelpImage, game_img, 400,250);
            HelpTitleLbl.Content = help_title;
        }

        private void SettingsItem_Click(object sender, RoutedEventArgs e)
        {
           LoadHelpContent(TAG_SETTINGS);
           //clearContent();
           loadHelpImage(HelpImage,settings_image, 400,215);
           clearImage(HelpImage2);
           HelpTitleLbl.Content = settings_title;
        }

        private void BackToMainHelpPageBtn_Click(object sender, RoutedEventArgs e)
        {
            Window1 mainWindow = new Window1();
            mainWindow.Show();
            this.Close();
        }

        private void LoadHelpContent(String help_tag)
        {
            if (File.Exists(helpFileName))
            {
                try
                {
                    XmlDocument document = new XmlDocument();
                    document.Load(helpFileName);

                    XmlNodeList nodeList = document.GetElementsByTagName(help_tag);
                    if (nodeList.Count != 1) throw new Exception();
                    HelpTextBlock.Text = nodeList[0].InnerText;

                }
                catch (Exception err)
                {
                    MessageBox.Show("Error reading help!" + err.Message);
                }
            }
            else
            {
                MessageBox.Show("Help file not available!");
            }
        }

        private void loadHelpImage(Image helpImg, String sourceFile, int h, int w)
        {
            
            Stream imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
            PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];
            if (HelpImage != null)
            {
                helpImg.Height = h;
                helpImg.Width = w;
                helpImg.Source = bitmapSource;
            }
            else
                helpImg.Source = null;
        }

        private void clearContent()
        {
            HelpTextBlock.Text = "";
        }

        private void clearImage(Image img)
        {
            img.Source = null;
        }

        private void MapItem_Click(object sender, RoutedEventArgs e)
        {
            HelpTitleLbl.Content = gameMap_title;
            LoadHelpContent(TAG_MAP);
            loadHelpImage(HelpImage, gameboard_img, 300, 350);
            clearImage(HelpImage2);
        }

        private void GameStartItem_Click(object sender, RoutedEventArgs e)
        {
            HelpTitleLbl.Content = startGame_title;
            LoadHelpContent(TAG_STARTGAME);
            loadHelpImage(HelpImage, startgame_img, 400, 350);
            //clearImage(HelpImage2);
        }

        private void GetHelpItem_Click(object sender, RoutedEventArgs e)
        {
            HelpTitleLbl.Content = getHelp_title;
            LoadHelpContent(TAG_GETHELP);
            loadHelpImage(HelpImage, helpCh_img, 160, 350);
            loadHelpImage(HelpImage2, buttons_img, 40, 350);
        }

        private void BonusesItem_Click(object sender, RoutedEventArgs e)
        {
            HelpTitleLbl.Content = bonuses_title;
            LoadHelpContent(TAG_BONUSESANDSTAGECOACHES);
            loadHelpImage(HelpImage, bonuses_img, 300, 350);
            clearImage(HelpImage2);
        }

        private void TownsItem_Click(object sender, RoutedEventArgs e)
        {
            HelpTitleLbl.Content = towns_title;
            LoadHelpContent(TAG_TOWNSANDROUTES);
            loadHelpImage(HelpImage, routes_img, 400, 350);
            clearImage(HelpImage2);
        }

        private void EndGameItem_Click(object sender, RoutedEventArgs e)
        {
            HelpTitleLbl.Content = endGame_title;
            LoadHelpContent(TAG_ENDGAME);
            clearImage(HelpImage);
            clearImage(HelpImage2);
        }

        private void PickCardItem_Click(object sender, RoutedEventArgs e)
        {
            HelpTitleLbl.Content = userThurn_title;
            LoadHelpContent(TAG_PICKCARD);
            loadHelpImage(HelpImage, sixcards_img, 220, 350);
            loadHelpImage(HelpImage2,cards_img , 150, 350);
        }

        private void MakeRouteItem_Click(object sender, RoutedEventArgs e)
        {
            HelpTitleLbl.Content = userThurn_title;
            LoadHelpContent(TAG_PLACECARDONROUTE);
            loadHelpImage(HelpImage, route_img, 150, 350);
            clearImage(HelpImage2);
        }

        private void ScoreItem_Click(object sender, RoutedEventArgs e)
        {
            HelpTitleLbl.Content = userThurn_title;
            LoadHelpContent(TAG_SCORE);
            loadHelpImage(HelpImage, buttons_img, 50, 350);
            clearImage(HelpImage2);
        }

        private void DiscardCardsItem_Click(object sender, RoutedEventArgs e)
        {
            HelpTitleLbl.Content = userThurn_title;
            LoadHelpContent(TAG_DISCARDCARDS);
            loadHelpImage(HelpImage, buttons_img, 50, 350);
            clearImage(HelpImage2);
        }

        private void EndTurnItem_Click(object sender, RoutedEventArgs e)
        {
            HelpTitleLbl.Content = userThurn_title;
            LoadHelpContent(TAG_ENDTURN);
            loadHelpImage(HelpImage, buttons_img, 50, 350);
            clearImage(HelpImage2);
        }
      
    }
}
